package com.dxjy.demo.exception;

import com.dxjy.demo.entity.R;
import com.dxjy.demo.entity.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: zhang
 * @Date: 2019/3/8 18:22
 * @Description:
 * 当我们写的接口拥有@RequiresRoles， @RequiresPermissions注解时，
 * 如果请求没有带有 token 或者带了 token 但权限认证不通过，
 * 则会报 UnauthenticatedException 异常，
 *  ExceptionController 类对这些异常进行了集中处理
 */
@Slf4j
@RestControllerAdvice
public class ExceptionController {


    /**
     * 捕捉shiro的异常
     * @return
     */
    @ExceptionHandler(ShiroException.class)
    public R handle401() {
        return new R().error(ResponseCode.NO_PERMISSION.getCode(),
                ResponseCode.NO_PERMISSION.getMessage());
    }

    /**
     * header错误
     * @param ex
     * @return
     */
    @ExceptionHandler(MissingRequestHeaderException.class)
    public R noHeaher(Throwable ex) {
        return new R().error(ResponseCode.MISS_HEADER.getCode(),
                ResponseCode.MISS_HEADER.getMessage()+ex.getMessage());
    }

    /**
     * 请求参数异常
     */
    @ExceptionHandler({HttpMessageNotReadableException.class,
            MissingServletRequestParameterException.class,
            MissingServletRequestPartException.class, BindException.class})
    public R parameterException(Throwable ex) {
        return new R().error(ResponseCode.PARAMETER_ERROR.getCode(),
                ResponseCode.PARAMETER_ERROR.getMessage() + ex.getMessage());
    }

    /**
     * 请求路径无法找到异常
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public R notFoundException(Throwable ex) {
        return new R().error(ResponseCode.NOT_FOUND.getCode(),
                ResponseCode.NOT_FOUND.getMessage() + ex.getMessage());
    }

    /**
     * 请求方法不支持异常
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R httpRequestMethodNotSupportedException(Throwable ex) {
        return new R().error(ResponseCode.METHOD_NOT_SUPPORTED.getCode(),
                ResponseCode.METHOD_NOT_SUPPORTED.getMessage() + ex.getMessage());
    }

    /**
     * 捕捉其他所有异常
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R globalException(HttpServletRequest request, Throwable ex) {
        log.error("系统异常：", ex);
        return new R().error(getStatus(request).value(), ex.getMessage());
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }
}